<template>
  <div class="app-container">
    <!-- <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="客户编号" prop="custid">
        <el-input
          v-model="queryParams.custid"
          placeholder="请输入客户编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="意向业态" prop="termtypename">
        <el-input
          v-model="queryParams.termtypename"
          placeholder="请输入意向业态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房间名称" prop="hourseid">
        <el-input
          v-model="queryParams.hourseid"
          placeholder="请输入房间名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="户型名称" prop="modelid">
        <el-input
          v-model="queryParams.modelid"
          placeholder="请输入户型名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="付款方式" prop="paidtypeid">
        <el-input
          v-model="queryParams.paidtypeid"
          placeholder="请输入付款方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="意向总价" prop="plantotalprice">
        <el-input
          v-model="queryParams.plantotalprice"
          placeholder="请输入意向总价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="意向单价" prop="planunitprice">
        <el-input
          v-model="queryParams.planunitprice"
          placeholder="请输入意向单价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="意向面积" prop="planarea">
        <el-input
          v-model="queryParams.planarea"
          placeholder="请输入意向面积"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="优先级" prop="priority">
        <el-input
          v-model="queryParams.priority"
          placeholder="请输入优先级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注" prop="demo">
        <el-input
          v-model="queryParams.demo"
          placeholder="请输入备注"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['house_sale:CustPlan:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['house_sale:CustPlan:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['house_sale:CustPlan:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['house_sale:CustPlan:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="CustPlanList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="意向编号" align="center" prop="planid" />
      <el-table-column label="客户编号" align="center" prop="custid" />
      <el-table-column label="意向业态" align="center" prop="termtypename" />
      <el-table-column label="意向房产" align="center" prop="hourseid" />
      <el-table-column label="意向户型" align="center" prop="modelid" />
      <el-table-column label="付款方式" align="center" prop="paidtypeid" />
      <el-table-column label="意向总价" align="center" prop="plantotalprice" />
      <el-table-column label="意向单价" align="center" prop="planunitprice" />
      <el-table-column label="意向面积" align="center" prop="planarea" />
      <el-table-column label="优先级" align="center" prop="priority" />
      <el-table-column label="备注" align="center" prop="demo" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['house_sale:CustPlan:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['house_sale:CustPlan:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户意向对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="意向业态" prop="termtypename">
           <el-select v-model="form.termtypename" placeholder="请选择意向业态"  >
            <el-option 
                v-for="type in termtypenames"
                :key="type.termtypename"
                :label="type.label"
                :value="type.termtypename">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="意向房产" prop="hourseid">
          <el-input v-model="form.hourseid" placeholder="请输入房间名称" />
        </el-form-item>
        <el-form-item label="意向户型" prop="modelid">
           <el-select v-model="form.modelid" placeholder="请选择意向户型"  >
            <el-option 
                v-for="type in modelids"
                :key="type.modelid"
                :label="type.label"
                :value="type.modelid">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="付款方式" prop="paidtypeid">
          <el-select v-model="form.paidtypeid" placeholder="请选择意向业态"  >
            <el-option 
                v-for="type in paidtypeids"
                :key="type.paidtypeid"
                :label="type.label"
                :value="type.paidtypeid">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="意向总价" prop="plantotalprice">
          <el-input v-model="form.plantotalprice" placeholder="请输入意向总价" />
        </el-form-item>
        <el-form-item label="意向单价" prop="planunitprice">
          <el-input v-model="form.planunitprice" placeholder="请输入意向单价" />
        </el-form-item>
        <el-form-item label="意向面积" prop="planarea">
          <el-select v-model="form.planarea" placeholder="请选择意向面积"  >
            <el-option 
                v-for="type in planareas"
                :key="type.planarea"
                :label="type.label"
                :value="type.planarea">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="优先级" prop="priority">
         <el-select v-model="form.priority" placeholder="请选择意向业态"  >
            <el-option 
                v-for="type in prioritys"
                :key="type.priority"
                :label="type.label"
                :value="type.priority">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="demo">
          <el-input v-model="form.demo" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCustPlan, getCustPlan, delCustPlan, addCustPlan, updateCustPlan } from "@/api/house_sale/CustPlan";

export default {
  name: "CustPlan",
  data() {
    return {

      termtypenames: [{
        termtypename:'普通住宅',
        label:'普遍住宅'
      },{
        termtypename:'商品房',
        label:'商品房'
      },{
        termtypename:'店铺',
        label:'店铺'
      },{
        termtypename:'写字楼',
        label:'写字楼'
      },{
        termtypename:'车库',
        label:'车库'
      },{
        termtypename:'别墅',
        label:'别墅'
      }],
      modelids:[{
         modelid:'一室一厅',
         label:'一室一厅'
      },{
         modelid:'两室两厅一卫',
         label:'两室两厅一卫'
      },{
         modelid:'跃层两室两厅',
         label:'跃层两室两厅'
      },{
         modelid:'四室两厅两卫',
         label:'四室两厅两卫'
      },{
         modelid:'三室两厅两卫',
         label:'三室两厅两卫'
      }],
      paidtypeids:[{
        paidtypeid:'现金',
        label:'现金'
      },{
        paidtypeid:'微信',
        label:'微信'
      },{
        paidtypeid:'支付宝',
        label:'支付宝'
      },{
        paidtypeid:'银行贷款',
        label:'银行贷款'
      },{
        paidtypeid:'pos刷卡',
        label:'pos刷卡'
      },{
        paidtypeid:'诚意金转账',
        label:'诚意金转账'
      },{
        paidtypeid:'pos机转账',
        label:'pos机转账'
      }],
      prioritys:[{
        priority:'高',
        label:'高'
      },{
        priority:'中',
        label:'中'
      },{
        priority:'低',
        label:'低'
      }],
      planareas:[{
        planarea:'50以下',
        label:'50以下'
      },{
        planarea:'51-80',
        label:'51-80'
      },{
        planarea:'81-90',
        label:'81-90'
      },{
        planarea:'91-100',
        label:'91-100'
      },{
        planarea:'101-120',
        label:'101-120'
      },{
        planarea:'121-140',
        label:'121-140'
      },{
        planarea:'141-160',
        label:'141-160'
      },{
        planarea:'161-200',
        label:'161-200'
      },{
        planarea:'201-300',
        label:'201-300'
      },{
        planarea:'301-400',
        label:'301-400'
      }],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户意向表格数据
      CustPlanList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        custid: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // // custid: [
        // //   { required: true, message: "客户编号不能为空", trigger: "blur" }
        // // ],
        // // termtypename: [
        // //   { required: true, message: "意向业态不能为空", trigger: "blur" }
        // // ],
        // hourseid: [
        //   { required: true, message: "房间名称不能为空", trigger: "blur" }
        // ],
        // // modelid: [
        // //   { required: true, message: "户型名称不能为空", trigger: "blur" }
        // // ],
        // // paidtypeid: [
        // //   { required: true, message: "付款方式不能为空", trigger: "blur" }
        // // ],
        plantotalprice: [
          { required: true, message: "意向总价不能为空", trigger: "blur" }
        ],
        planunitprice: [
          { required: true, message: "意向单价不能为空", trigger: "blur" }
        ],
        // planarea: [
        //   { required: true, message: "意向面积不能为空", trigger: "blur" }
        // ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询客户意向列表 */
    getList() {
      this.loading = true;
       this.queryParams.custid=this.$route.query.id;
      listCustPlan(this.queryParams).then(response => {
        this.CustPlanList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
     const custid=this.$route.query.id;
      console.log(custid);
      this.form = {
        planid: null,
        custid: custid,
        termtypename: this.termtypenames[0].termtypename,
        hourseid: null,
        modelid: this.modelids[0].modelid,
        paidtypeid: this.paidtypeids[0].paidtypeid,
        plantotalprice: null,
        planunitprice: null,
        planarea: this.planareas[0].planarea,
        priority: this.prioritys[0].priority,
        demo: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.planid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户意向";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const planid = row.planid || this.ids
      getCustPlan(planid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户意向";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.planid != null) {
            updateCustPlan(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCustPlan(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const planids = row.planid || this.ids;
      this.$modal.confirm('是否确认删除客户意向编号为"' + planids + '"的数据项？').then(function() {
        return delCustPlan(planids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('house_sale/CustPlan/export', {
        ...this.queryParams
      }, `CustPlan_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
